//
//  SceneDelegate.swift
//  BaoDan
//
//  Created by kra on 2024/6/5.
//

import BDiOS
import BDKit
import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate, UISceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions
    ) {
        guard let windowScene = (scene as? UIWindowScene) else { return }

        WXApi.startLog(by: .normal) { log in
            print(log)
        }
//        WXApi.checkUniversalLinkReady { (step, result) in
//            print(
//                "universal link ready: \(step). if success: \(result.success); or error: \(result.errorInfo); and suggestion: \(result.suggestion)"
//            )
//          
//        }
        makeRootWindow(windowScene: windowScene)
        
        // 处理 URL
        if let url = connectionOptions.urlContexts.first?.url {
            print("📥 冷启动 URL: \(url)")
            
            if url.scheme?.lowercased() == "baodanex",
               url.host == "openSettings" {
                if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
                    UIApplication.shared.open(settingsURL)
                    
                }
            }
        }
        
//        if window == nil {
//            let window = UIWindow(windowScene: windowScene)
//          
//            if UserDefaults.baodan.string(forKey: UserDefaults.tokenKey) != nil {
//                window.rootViewController = MainTabBarController()
//            } else {
//                window.rootViewController = WelcomeViewController()
//            }
//
//            self.window = window
//            window.makeKeyAndVisible()
//        }
//        let url = connectionOptions.urlContexts.first?.url
//        print("第一个：\(String(describing: url))")

        /// 外部导入 zip 文件
//        if let url = connectionOptions.urlContexts.first?.url {
//            if url.pathExtension.lowercased() == "zip" {
//                Task {
//                    BaoDanAppDependencyContainer.shared.mainViewModel
//                        .pushInputSchema()
//                    await BaoDanAppDependencyContainer.shared.inputSchemaViewModel
//                        .importZipFile(fileURL: url)
//                }
//                return
//            }
//
//            // url.query(): 获取 `URL` 查询参数
//            // url.lastPathComponent 获取 `URL` 中 `/a/b` 中最后一个 b
//            let components = url.lastPathComponent
//            if let subView = BDSettingsSubView(rawValue: components) {
//                BaoDanAppDependencyContainer.shared.mainViewModel
//                    .navigation(subView)
//            }
//        }

        // 通过快捷方式打开
//        if let shortItem = connectionOptions.shortcutItem,
//           let shortItemType = ShortcutItemType(
//            rawValue: shortItem.localizedTitle
//           ),
//           shortItemType != .none
//        {
//            BaoDanAppDependencyContainer.shared.mainViewModel
//                .pushRIME()
//            BaoDanAppDependencyContainer.shared.mainViewModel
//                .execShortcutCommand(shortItemType)
//        }
    }
    
    private func makeRootWindow(windowScene: UIWindowScene) {
        if window == nil {
            let window = UIWindow(windowScene: windowScene)
          
            if UserDefaults.baodan.string(forKey: UserDefaults.tokenKey) != nil {
                window.rootViewController = MainTabBarController()
            } else {
                window.rootViewController = WelcomeViewController()
            }

            self.window = window
            window.makeKeyAndVisible()
        }
    }

    // 通过URL打开App
    func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        guard let windowScene = (scene as? UIWindowScene) else { return }

        makeRootWindow(windowScene: windowScene)
      
        guard let urlContext = URLContexts.first else { return }
        let url = urlContext.url
        print("🚀 SceneDelegate 热启动 URL: \(url)")
        
        if url.scheme?.lowercased() == "baodanex",
           url.host == "openSettings" {
            if let settingsURL = URL(
                string: UIApplication.openSettingsURLString
            ) {
                UIApplication.shared.open(settingsURL)
            }
            return
        }
        if url.scheme?.lowercased() == "baodanex",
           url.host == "share" {
            handleIncomingURL(url)
            return
        }
  
        WXApi.handleOpen(url, delegate: WXApiManager.shared)
        /// 外部导入 zip 文件
//        if let url = URLContexts.first?.url {
//            if url.pathExtension.lowercased() == "zip" {
//                Task {
//                    BaoDanAppDependencyContainer.shared.mainViewModel
//                        .pushInputSchema()
//                    await BaoDanAppDependencyContainer.shared.inputSchemaViewModel
//                        .importZipFile(fileURL: url)
//                }
//                return
//            }
//
//            // url.query(): 获取 `URL` 查询参数
//            // url.lastPathComponent 获取 `URL` 中 `/a/b` 中最后一个 b
//            let components = url.lastPathComponent
//            if let subView = BDSettingsSubView(rawValue: components) {
//                BaoDanAppDependencyContainer.shared.mainViewModel
//                    .navigation(subView)
//            }
//        }
    }
    
    func handleIncomingURL(_ url: URL) {
        if let result = HostURLParser.parse(url: url) {
            WXApi.shareAction(share: result)
        } else {
            print("解析 URL 失败")
        }
    }

    /// 程序已启动下，通过 quick action 打开
//    func windowScene(
//        _ windowScene: UIWindowScene,
//        performActionFor shortcutItem: UIApplicationShortcutItem,
//        completionHandler: @escaping (Bool) -> Void
//    ) {
//        guard let window = window else { return }
//        guard let rootController = window.rootViewController else { return }
//        guard let mainViewController = rootController as? MainViewController else {
//            return
//        }
//
//        mainViewController.navigationController?
//            .popViewController(animated: false)
//
//        // 通过快捷方式打开
//        if let shortItemType = ShortcutItemType(rawValue: shortcutItem.localizedTitle), shortItemType != .none {
//            BaoDanAppDependencyContainer.shared.mainViewModel
//                .pushRIME()
//            BaoDanAppDependencyContainer.shared.mainViewModel
//                .execShortcutCommand(shortItemType)
//        }
//    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    /// 应用注册 quick action
    func sceneWillResignActive(_ scene: UIScene) {
        //    let application = UIApplication.shared
        //    let rimeDeploy = UIApplicationShortcutItem(type: "RIME", localizedTitle: ShortcutItemType.rimeDeploy.rawValue)
        //    let rimeSync = UIApplicationShortcutItem(type: "RIME", localizedTitle: ShortcutItemType.rimeSync.rawValue)
        ////    let rimeReset = UIApplicationShortcutItem(type: "RIME", localizedTitle: ShortcutItemType.rimeReset.rawValue)
        //    application.shortcutItems = [rimeDeploy, rimeSync]
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }
    
    func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
        WXApi.handleOpenUniversalLink(userActivity, delegate: self)
    }
}

extension SceneDelegate: WXApiDelegate {
    
}
